
var SFXManager = cc.Class({
    extends: cc.Component,

    properties: {
		MainMenuBGM: cc.AudioSource,
		BattleBGM: cc.AudioSource,
        soundEffects: [cc.AudioSource],
		BGMVolume: 0,
		SFXVolume: 0,
    },

	statics: {
		instance: null,
	},
	ctor() {
		SFXManager.instance = this;
	},

	onLoad () {
		this.BGMVolume = window.localStorage.getItem("BGMVolume");
		this.SFXVolume = window.localStorage.getItem("SFXVolume");
		this.MainMenuBGM.volume = this.BGMVolume;
		this.BattleBGM.volume = this.BGMVolume;
		if (cc.director.getScene().name == "MainMenu") {
			this.MainMenuBGM.playOnLoad = true;
		}
		if (cc.director.getScene().name == "Main") {
			this.BattleBGM.playOnLoad = true;
		}

		for (let i = 0; i < this.soundEffects.length; i++) {
			this.soundEffects[i].volume = this.SFXVolume;
		}
	},

    playSFX (sfxToPlay) {
		this.soundEffects[sfxToPlay].stop();
		this.soundEffects[sfxToPlay].play();
		
	},

	setBGMVolume () {
		this.BGMVolume = window.localStorage.getItem("BGMVolume");
		this.SFXVolume = window.localStorage.getItem("SFXVolume");
		this.MainMenuBGM.volume = this.BGMVolume;
		this.BattleBGM.volume = this.BGMVolume;
	},
});

module.exports = SFXManager;
